﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace OrBitDataModule.Common
{
    public class SettingDictionary : Dictionary<string, object>
    {
        public SettingDictionary(object obj)
        {
            PropertyInfo[] properties = obj.GetType().GetProperties();

            foreach (PropertyInfo property in properties)
            {
                string key = property.Name;
                AddPair(key, property.GetValue(obj));
            }
        }
        // 添加键值对
        private void AddPair(string key, object value)
        {
            this[key] = value;
        }

        // 获取值（泛型方法）
        public T GetValue<T>(string key)
        {
            if (ContainsKey(key))
            {
                return (T)Convert.ChangeType(this[key], typeof(T));
            }
            else
            {
                throw new KeyNotFoundException("Key not found in dictionary");
            }
        }

        // 将类对象的属性添加到字典中
        private void AddProperties(object obj)
        {
            PropertyInfo[] properties = obj.GetType().GetProperties();

            foreach (PropertyInfo property in properties)
            {
                string key = property.Name;
                AddPair(key, property.GetValue(obj));
            }
        }
    }
}
